<?php

namespace Kingbes\Gui\Control;

use Kingbes\Raylib\Utils;
use \FFI\CData;

class Base
{
    // 控件ID
    public int $id;
    // 控件class
    public string $class;
    // 控件标题
    public string $title = "";
    // 默认字体大小
    public int $size = 16;
    // 默认字体间距
    public int $letterSpacing = 0;
    // 默认背景色-白色
    public CData $backgroundColor;
    // 默认字体颜色-黑色
    public CData $fontColor;
    // 鼠标是否悬停
    public bool $onmouseover = false;
    // 鼠标点击
    public bool $onclick = false;
    // 是否可见
    public bool $visible = true;
    // 控件宽度
    public int $width = 0;
    // 控件高度
    public int $height = 0;
    // 控件边框颜色
    public CData $borderColor;
    // 控件边框宽度
    public int $borderWidth = 0;
    // 控件边框圆角
    public int $borderRadius = 0;

    public function __construct()
    {
        // 初始化背景色-白色
        $this->backgroundColor = Utils::color(255, 255, 255, 255);
        // 初始化字体颜色-黑色
        $this->fontColor = Utils::color(0, 0, 0, 255);
        // 初始化边框颜色-黑色
        $this->borderColor = Utils::color(0, 0, 0, 255);
    }

    /**
     * 设置字体大小
     *
     * @param integer $size
     * @return void
     */
    public function setSize(int $size): void
    {
        if ($size > 72) {
            $this->size = 72;
        } else if ($size < 12) {
            $this->size = 12;
        } else {
            $this->size = $size;
        }
    }

    /**
     * 设置背景颜色
     *
     * @param array<int, int> $color 颜色值
     * @return void
     */
    public function setBackgroundColor(array $color): void
    {
        if (count($color) != 4) {
            throw new \Exception("颜色值必须包含4个元素");
        }
        $this->backgroundColor = Utils::color($color[0], $color[1], $color[2], $color[3]);
    }

    /**
     * 设置字体颜色
     *
     * @param array<int, int> $color 颜色值
     * @return void
     */
    public function setFontColor(array $color): void
    {
        if (count($color) != 4) {
            throw new \Exception("颜色值必须包含4个元素");
        }
        $this->fontColor = Utils::color($color[0], $color[1], $color[2], $color[3]);
    }

    /**
     * 设置边框颜色
     *
     * @param array<int, int> $color
     * @return void
     */
    public function setBorderColor(array $color): void
    {
        if (count($color) != 4) {
            throw new \Exception("颜色值必须包含4个元素");
        }
        $this->borderColor = Utils::color($color[0], $color[1], $color[2], $color[3]);
    }

    /**
     * 获取属性值
     *
     * @param string $name 属性名
     * @return mixed
     */
    public function __get(string $name): mixed
    {
        if (
            $name === "backgroundColor"
            || $name === "fontColor"
            || $name === "borderColor"
        ) {
            return [
                $this->backgroundColor->r,
                $this->backgroundColor->g,
                $this->backgroundColor->b,
                $this->backgroundColor->a,
            ];
        } else {
            return $this->$name;
        }
    }

    /**
     * 设置属性值
     *
     * @param string $name 属性名
     * @param mixed $value 属性值
     * @return void
     */
    public function __set(string $name, mixed $value): void
    {
        if (
            $name === "backgroundColor"
            || $name === "fontColor"
            || $name === "borderColor"
        ) {
            $this->$name = Utils::color($value[0], $value[1], $value[2], $value[3]);
        } else {
            $this->$name = $value;
        }
    }
}
